/*
 * Copyright (C) 2009 Rafael Fernandes
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.phonebooksharing.android.activities.dashboard;

import java.io.File;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.phonebooksharing.android.R;
import com.phonebooksharing.android.util.Global;
import com.phonebooksharing.android.util.IOUtilities;

public class VideoReaderActivity extends AbstractReaderActivity {

    public static final String VIDEO_ID              = "id",
                               VIDEO_LONGITUDE       = "lo",
                               VIDEO_LATITUDE        = "la",
                               VIDEO_TITLE           = "tl",
                               VIDEO_FILENAME        = "fn",
                               VIDEO_DURATION        = "vd";
    
//    private int photoW, photoH, screenW, screenH;
//    private long photoID;
    private View container;
    private ImageView photo;
    private TextView title, where, resolution;
    private File fileName;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        // Handle the calling intent
        final Intent i = getIntent();
        container = ((ViewStub)findViewById(R.id.photoStub)).inflate();
        
        photo = (ImageView)container.findViewById(R.id.icon);
        photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                invokeVideoReader();
            }
        });
        
        title = (TextView)container.findViewById(R.id.text1);
        where = (TextView)container.findViewById(R.id.text2);
        resolution = (TextView)container.findViewById(R.id.text3);
        
        photo.setImageBitmap(Global.tempBitmap.getBitmap());
        title.setText(i.getStringExtra(VIDEO_TITLE));
//        where.setText("" + values[2]);
        where.setVisibility(View.GONE);
        resolution.setText(i.getStringExtra(VIDEO_DURATION));
        
        ((TextView)container.findViewById(android.R.id.text1)).setText("Duration: ");
        
        ((TextView)findViewById(R.id.title)).setText("Video Thumbnail");
        fileName = new File(i.getStringExtra(VIDEO_FILENAME));
        
        invokeVideoReader();
    }

    private void invokeVideoReader() {
        try {
            Intent av = new Intent(Intent.ACTION_VIEW);
            av.setDataAndType(Uri.fromFile(fileName), "video/*");
            startActivity(av);
        } catch(ActivityNotFoundException a) {
            Toast.makeText(this, "We're sorry but it seems that you don't have a Video Player!", Toast.LENGTH_LONG).show();
        }
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Global.setTempBitmap(null);
        IOUtilities.removeExternalFile(fileName);
    }
}
